﻿package game 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import game.events.EventData;
	import flash.display.LoaderInfo;
	import flash.utils.setTimeout;
	import flash.external.ExternalInterface;
	/**
	 * ...
	 * @author ... Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class GameDetail extends MovieClip
	{
		
		public var menuMain:GameMenuPanel;
		public var gameChild:MovieClip;
		
		public var loadGameFirst:LoadMc;
		private var gameLocalconnection:GameLocalConnection;
		private var gameName:String = "bac.swf";
		private static const loadCompletedSWF:String = "loadCompletedSWF";
		private var timeLoadGame:String;
		
		private var channelControl:ChannelControl;		
		
		public var daisy:TextField = new TextField();
		
		public function GameDetail() 
		{
			
			this.loadGameFirst = new LoadMc();
			this.gameLocalconnection = new GameLocalConnection();
			this.addEventListener(EventData.SET_MENU_GAME, menuGameHdl);
			this.channelControl = new ChannelControl();
			ExternalInterface.addCallback("openRightClick", openRightClick);
			
			this.addEventListener(EventData.CHANGE_LOAD_GAME, changeLoadGameHdl);
			daisy.visible = false;
			
			/**
			 * Debug
			 */
			
			loadGame();
			this.channelControl.initFmsMonitor(GlobalVariables.fmsMonitor);
			this.channelControl.addEventListener(EventData.SET_FMS_CHANNEL, updateVideoHdl);
			
			/**
			* End debug
			*/
		 
			/**
			* Run web
			*/
			/*
			readFlashVar();
			this.loaderInfo.addEventListener(Event.INIT, hdlInitLC);
			this.gameLocalconnection.addEventListener(EventData.LOAD_GAME, hdlLoadGame);
			*/
			/**
			 * End web
			 */
		}		
		
		private function openRightClick():void {	
			
			callChild("rightClick");
		}
		
		private function updateVideoHdl(e:EventData):void 
		{
			trace("updateVideoHdl");
			callChild("activeVideo");
		}
		
		private function callChild(nameFunction:String):void {
			
			switch(nameFunction){ 	
				case "activeVideo":
					this.loadGameFirst.movieChild.activeVideo(this.channelControl);
					break;
				case GameDetail.loadCompletedSWF:
				case menuMain.accountInfoBtn.name:
				case menuMain.betStatusBtn.name: 
				case menuMain.gameHistoryBtn.name:
				case menuMain.gameRuleBtn.name:
				case menuMain.supportBtn.name:
					trace("nameFunction:" + nameFunction);
					this.loadGameFirst.movieChild.receiverInfoFromMainGame(nameFunction);
					break;
				case "rightClick":
					this.loadGameFirst.movieChild.rightClick();
					break;
				case "closeConnect":
					this.loadGameFirst.movieChild.closeConnect();
					break;
			}
		}
		
		private function readFlashVar():void {
			var paramObj:Object = LoaderInfo(root.loaderInfo).parameters;
			GlobalVariables.rtmpStr = paramObj["rtmp"];
			GlobalVariables.vodRtmp = paramObj["VodRtmp"];
			GlobalVariables.vpn = paramObj["VPN"];
			GlobalVariables.maxRo = paramObj["MaxRo"];
			GlobalVariables.getDate = paramObj["_getDate"];
			GlobalVariables.gameCard = paramObj["gameCard"];
			GlobalVariables.ip = paramObj["ip"];
			GlobalVariables.countryCode = paramObj["countrycode"];
			GlobalVariables.gameTableName = paramObj["gameType"];
			setGameNameByGameType(GlobalVariables.gameTableName);//check load GameType
			loadListGameValue(paramObj["listGame"]);	
			this.timeLoadGame = GlobalVariables.getDate;
		}
		
		
		private function hdlInitLC(e:Event):void 
		{
			setTimeout(initLC, 100);
		}
		
		private function initLC():void {
			this.gameLocalconnection.init("fromMain" + this.timeLoadGame, "fromGameDetail" + this.timeLoadGame);
			this.gameLocalconnection.sendInfoLoadComplete(GlobalVariables.getDate);
		}
		
		private function menuGameHdl(e:EventData):void 
		{
			trace("menuGameHdl:" + e.ms);
			switch(e.ms) {
				case menuMain.roulleteBtn.name:
					if (this.gameName != "roulette.swf") {
						callChild("closeConnect");
						GlobalVariables.roomId = "9";
						this.gameName = "roulette.swf";
						loadGame();
					}
					break;
				case menuMain.dragonBtn.name:
					if (this.gameName != "dt.swf") {
						callChild("closeConnect");
						GlobalVariables.roomId = "27";
						this.gameName = "dt.swf";
						loadGame();
					}
					break;
				case menuMain.bacaratBtn.name:
					if (this.gameName != "bac.swf") {
						callChild("closeConnect");
						GlobalVariables.roomId = "20";
						this.gameName = "bac.swf";
						loadGame();
					}
					break;
				case menuMain.sicboBtn.name:
					if (this.gameName != "sicbo.swf") {
						callChild("closeConnect");
						GlobalVariables.roomId = "6";
						this.gameName = "sicbo.swf";
						loadGame();
					}
					trace("menuMain.sicboBtn" + e.ms);
					break;
				case menuMain.bacInsBtn.name:
					if (this.gameName != "bacIns.swf") {
						callChild("closeConnect");
						GlobalVariables.roomId = "23";
						this.gameName = "bacIns.swf";
						loadGame();
					}
					break;
				case menuMain.lobbyBtn.name:
					ExternalInterface.call("CloseBrowser");
					break;
				case menuMain.accountInfoBtn.name:
					callChild(menuMain.accountInfoBtn.name);
					break;
				case menuMain.betStatusBtn.name:
					callChild(menuMain.betStatusBtn.name);
					break;
				case menuMain.gameHistoryBtn.name:
					callChild(menuMain.gameHistoryBtn.name);
					break;
				case menuMain.gameRuleBtn.name:
					callChild(menuMain.gameRuleBtn.name);
					break;
				case menuMain.supportBtn.name:
					callChild(menuMain.supportBtn.name);
					break
				
			}
		}
		
		
		private function hdlLoadGame(e:Event):void 
		{
			loadGame();
			//check chanel
		/*	if (init(GlobalVariables.roomId) > 20) {
				this.channelControl.initFmsMonitor(GlobalVariables.fmsMonitor1);
			}else {
				this.channelControl.initFmsMonitor(GlobalVariables.fmsMonitor);
			}*/
			//end check
			this.channelControl.initFmsMonitor(GlobalVariables.fmsMonitor);
			this.channelControl.addEventListener(EventData.SET_FMS_CHANNEL, updateVideoHdl);
		}
		
		private function loadGame():void {
			/*trace("GlobalVariables.roomId:" + GlobalVariables.roomId);
			if (GlobalVariables.roomId == "1")
				this.gameName = "bac.swf";
			else if (GlobalVariables.roomId == "6")
				this.gameName = "sicbo.swf";}*/
			try{
				this.loadGameFirst.loadSwf(gameName, gameChild);
				this.loadGameFirst.myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, hdlCompleteSWF);
			}catch(e:Error){ trace(e.errorID)}
		}
		
		private function hdlCompleteSWF(e:Event):void 
		{
			callChild(GameDetail.loadCompletedSWF);
		}
		
		/**
		 * @author Daisy
		 * @param	str
		 */		
		private function loadListGameValue(str:String):void {		
			var arr:Array = new Array();			
			arr = str.split("@");
			
			GlobalVariables.arrListGameAllName = new Array();
			GlobalVariables.arrListGameAllName = String(arr[0]).split(",");
			GlobalVariables.arrListGameAllTable = new Array();
			GlobalVariables.arrListGameAllTable = arr[1].split(",");
			GlobalVariables.arrListGameAllStatus = new Array();
			GlobalVariables.arrListGameAllStatus = String(arr[2]).split(",");
			
			GlobalVariables.arrListGameBacName = new Array();
			GlobalVariables.arrListGameBacTable = new Array();
			GlobalVariables.arrListGameBacStatus = new Array();			
			
			for (var i:int = 0; i < GlobalVariables.arrListGameAllName.length; i ++ ) {
				if ((String(GlobalVariables.arrListGameAllName[i]).toUpperCase() == "BAC") || (String(GlobalVariables.arrListGameAllName[i]).toUpperCase() == "BACINS")) {
					GlobalVariables.arrListGameBacName.push(GlobalVariables.arrListGameAllName[i]);
					GlobalVariables.arrListGameBacTable.push(GlobalVariables.arrListGameAllTable[i]);
					if (String(GlobalVariables.arrListGameAllStatus[i]).toUpperCase() == "OPEN") {
						GlobalVariables.arrListGameBacStatus.push(0);
					}else {
						GlobalVariables.arrListGameBacStatus.push(-1);
					}
				}
			}				
		}
		
		/**
		 * Check GameType to load game
		 * @param	gameType
		 */
		private function setGameNameByGameType(gameType:String):void {			
			switch (gameType.toUpperCase()) {
				case "BAC":
					this.gameName = "bac.swf";
					break;
				case "BACINS":
					this.gameName = "bacIns.swf";
					break;
				case "DT":
					this.gameName = "dt.swf";
					break;
				case "SIC":
					this.gameName = "sicbo.swf";
					break;
				case "SD":
					this.gameName = "sedie.swf";
					break;				   
				case "RO":
					this.gameName = "roulette.swf";
					break;
				case "FT":
					this.gameName = "sedie.swf";
			}
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 * @usage Change Load Game
		 */
		private function changeLoadGameHdl(e:EventData):void {			
			var roomTable:String = e.activeRoomBac;
			setGameName(roomTable);
			callChild("closeConnect");
			GlobalVariables.roomId = roomTable;
			loadGame();
		}		
		
		/**
		 * @author Daisy
		 * @param	table
		 */
		private function setGameName(table:String):void {			
			for (var i:int = 0; i < GlobalVariables.arrListGameAllTable.length; i++) {
				if (table == GlobalVariables.arrListGameAllTable[i]) {
					GlobalVariables.gameTableName = GlobalVariables.arrListGameAllName[i];
				}
			}
			setGameNameByGameType(GlobalVariables.gameTableName);
		}	
		
	}

}